package simportal.service;

import java.util.ArrayList;
import java.util.List;

import com.cfuture08.eweb4j.component.dwz.vo.NavMenu;
import com.cfuture08.eweb4j.component.dwz.vo.TreeMenu;
import com.cfuture08.eweb4j.orm.dao.factory.DAOFactory;

public class TreeMenuService extends BaseService<TreeMenu> {
	public TreeMenuService() {
		super(TreeMenu.class);
	}

	/**
	 * 给定一个TreeMenu列表，找出其符合某个navMenuId的子列表, 注意不能以自己为父菜单。
	 * 
	 * @param list
	 * @param navMenuId
	 * @return
	 */
	public List<TreeMenu> getTreeMenusByKeywordAndNavMenuIdAndPaging(
			String keyword, Integer treeMenuId, Integer navMenuId, Integer p,
			Integer n) {
		// 自定义条件
		String condition = "id <> ? and navMenuId = ? and name like ? ";
		Object[] args = new Object[] { treeMenuId, navMenuId,
				"%" + keyword + "%" };
		List<TreeMenu> pojos = DAOFactory.getDivPageDAO().divPageByWhere(
				TreeMenu.class, p, n, condition, args);

		if (pojos != null) {
			DAOFactory.getCascadeDAO().select(
					pojos.toArray(new TreeMenu[] { new TreeMenu() }), "parent",
					"navMenu");
		}
		
		if (pojos == null)
			pojos = new ArrayList<TreeMenu>();

		TreeMenu tm = new TreeMenu();
		tm.setTreeMenuId(0);
		tm.setName("主菜单-没有父节点");
		NavMenu navMenu = DAOFactory.getSelectDAO().selectOne(NavMenu.class,
				new String[] { "navMenuId" },
				new String[] { String.valueOf(navMenuId) });
		tm.setNavMenu(navMenu);
		pojos.add(tm);

		return pojos;
	}

	/**
	 * 分页查询，级联查询
	 * 
	 * @param p
	 * @param n
	 * @return
	 */
	public List<TreeMenu> getPageWithCascade(int p, int n) {
		List<TreeMenu> pojos = this.getPage(p, n);
		DAOFactory.getCascadeDAO().select(
				pojos.toArray(new TreeMenu[] { new TreeMenu() }), "parent",
				"navMenu");

		return pojos;
	}

	/**
	 * 返回某个导航菜单下的所有树形菜单列表
	 * 
	 * @param navMenuId
	 * @return
	 */
	public List<TreeMenu> getTreeMenusByNavMenuId(Integer treeMenuId,
			Integer navMenuId) {
		// 自定义查询条件
		String condition = "id <> ? and navMenuId = ? ";
		List<TreeMenu> pojos = DAOFactory.getSelectDAO().selectWhere(
				TreeMenu.class, condition, treeMenuId, navMenuId);
		if (pojos != null) {
			DAOFactory.getCascadeDAO().select(
					pojos.toArray(new TreeMenu[] { new TreeMenu() }), "parent",
					"navMenu");
		}

		if (pojos == null)
			pojos = new ArrayList<TreeMenu>();

		TreeMenu tm = new TreeMenu();
		tm.setTreeMenuId(0);
		tm.setName("主菜单-没有父节点");
		NavMenu navMenu = DAOFactory.getSelectDAO().selectOne(NavMenu.class,
				new String[] { "navMenuId" },
				new String[] { String.valueOf(navMenuId) });
		tm.setNavMenu(navMenu);
		pojos.add(tm);

		return pojos;
	}

	/**
	 * 更新,级联更新
	 * 
	 * @param pid
	 * @param pojo
	 * @return
	 */
	public String updateByCascade(TreeMenu pojo) {
		String info = null;
		// 级联修改
		info = update(pojo, "name", "菜单名");
		if (info == null) {
			DAOFactory.getCascadeDAO().update(pojo, "navMenu", "parent");
		} else {
			info = "操作失败," + info;
		}

		return info;
	}

	/**
	 * 添加，级联更新
	 * 
	 * @param pojo
	 * @param pk
	 * @param desc
	 * @return
	 */
	public String createWithCascade(TreeMenu pojo) {

		String result = null;
		result = create(pojo, "name", "菜单名");
		if (result == null) {
			DAOFactory.getCascadeDAO().update(pojo, "navMenu", "parent");
		} else {
			result = "添加失败，" + result;
		}
		return result;
	}
}
